{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "10cdeb27-ddcf-4515-9540-a6f6186fff90",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2f749d00-33a7-4139-af22-054a3f3468a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt = np.dtype(np.int32)\n",
    "dt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e80522da-d0be-43a9-9eb9-efa91ab6b04f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('age', 'i1')]\n"
     ]
    }
   ],
   "source": [
    "dt = np.dtype([(\"age\", np.int8)])\n",
    "print(dt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "454209ce-e8a4-44e7-a6e1-696ca0717a04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "a.shape = (3, 2)\n",
    "print(a)\n",
    "a.shape = (2, 3)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "84d16c7a-9cac-4290-843e-4f0087e1e67d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[b'H' b'e' b'l' b'l' b'o' b' ' b'W' b'o' b'r' b'l' b'd']\n"
     ]
    }
   ],
   "source": [
    "s = b\"Hello World\"\n",
    "a = np.frombuffer(s, dtype=\"S1\")\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d3f52e8c-dbe0-4f9c-b9c9-157fbb6619af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我们的数组是：\n",
      "[[ 0  1  2]\n",
      " [ 3  4  5]\n",
      " [ 6  7  8]\n",
      " [ 9 10 11]]\n",
      "\n",
      "\n",
      "这个数组的四个角元素是：\n",
      "[[ 0  2]\n",
      " [ 9 11]]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]])\n",
    "print(\"我们的数组是：\")\n",
    "print(x)\n",
    "print(\"\\n\")\n",
    "rows = np.array([[0, 0], [3, 3]])\n",
    "cols = np.array([[0, 2], [0, 2]])\n",
    "y = x[rows, cols]\n",
    "print(\"这个数组的四个角元素是：\")\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f42439b9-da82-49f9-87b2-887be5a3be46",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row = np.array([0])\n",
    "col = np.array([2])\n",
    "z = x[row, col]\n",
    "z"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25c2ec0c-d47a-4222-921e-d7a633641ae0",
   "metadata": {},
   "source": [
    "取 4 列数组 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "9371e578-418b-4127-85e9-7a2ee392c768",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  9, 10])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row1 = np.array([0, 0, 3, 3])\n",
    "col1 = np.array([0, 1, 0, 1])\n",
    "v = x[row1, col1]\n",
    "v"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e146f2c-54cd-4202-bd9e-629a95c0e6b3",
   "metadata": {},
   "source": [
    "取 4 行数组 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "76c7a20b-d96d-45f6-b18c-59421d95a14f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2],\n",
       "       [ 4],\n",
       "       [ 6],\n",
       "       [11]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row2 = np.array([[0], [1], [2], [3]])\n",
    "col2 = np.array([[2], [1], [0], [2]])\n",
    "v1 = x[row2, col2]\n",
    "v1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "03adaf18-5f31-41e5-b35c-6fa4168b939c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]]\n",
      "[0 1 2 3 4 5]\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(12)\n",
    "arr.shape = (2,6)\n",
    "print(arr)\n",
    "print(arr[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "ae8ebf5f-911c-489c-848c-07647f1f9283",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  5],\n",
       "       [ 6, 11]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rows = np.array([[0,0],[1,1]])\n",
    "cols = np.array([[0,5],[0,5]])\n",
    "corners = arr[rows,cols]\n",
    "corners"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d438922-6ff7-4102-b8a3-48f2b8883ecb",
   "metadata": {},
   "source": [
    "`arr[0][2] == arr[0, 2]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "55a4547c-e7e0-4cba-a8f7-22a43a63980d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr[0][2]:   2\n",
      "arr[0, 2]:   2\n"
     ]
    }
   ],
   "source": [
    "print(f\"arr[0][2]: {arr[0][2]:>3}\")\n",
    "print(f\"arr[0, 2]: {arr[0,2]:>3}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef09e44d-4c29-4f3c-90d7-ac41c5b4fcd3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Py(Python note)",
   "language": "python",
   "name": "pynote"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
